package com.zhaoxi.project.wms.service;

import com.zhaoxi.common.utils.DateUtils;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.project.wms.mapper.WarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//样例
@Service
public class WarehouseService extends BaseService {
  @Autowired
  private WarehouseMapper warehouseMapper;
  //查询数据集合
  public PageData getList(PageData pd) {
    PageData res = new PageData();
    List<PageData> list = warehouseMapper.getList(pd);
    return this.responses(list);
    
  }
  //查询数据行数
  public PageData getCount(PageData pd) {
    PageData res = new PageData();
    return this.success(warehouseMapper.getCount(pd));
    
  }
  //新增
  public PageData add(PageData pd) {
    if (pd.getLong("parentId") == null){
      pd.put("parentId",0);
    }
//    获取父类的祖籍
    PageData pd1 = new PageData();
    pd1.put("id",pd.getLong("parentId"));
    List<PageData> pdList = warehouseMapper.getList(pd1);
    pd1.clear();
    if (pdList.size()>0){
      pd1 = pdList.get(0);
    }else{
      pd1.put("ancestors","0,");
    }
    String ancestors = pd1.getString("ancestors");
    pd.put("ancestors",ancestors+pd.getString("parentId")+",");
    //插入数据
    pd.put("createTime",DateUtils.getNowDate());
    pd.put("createBy",this.getUsername());
    //返回值
    PageData res = new PageData();
    return this.responses(warehouseMapper.add(pd));
    
  }
  //修改
  public PageData edit(PageData pd) {
    pd.put("updateBy",this.getUsername());
    PageData res = new PageData();
    return this.responses(warehouseMapper.edit(pd));
    
  }
  //删除
  public PageData del(Long id) {
    PageData res = new PageData();
    return this.responses(warehouseMapper.del(id));
    
  }
}
